<?php

namespace app\admin\controller\yungift;

use addons\yungift\library\Super;
use app\common\controller\Backend;
use app\admin\model\yungift\Feedback as FeedbackModel;
use \app\admin\model\yungift\Activity;
use \app\admin\model\yungift\Member;

/**
 * 意见反馈模块
 */
class Feedback extends Backend
{
    /**
     * @var FeedbackModel
     */
    protected $model;

    // 联表查询，解决字段冲突
    protected $relationSearch = true;

    // 初始化
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new FeedbackModel();

        $this->view->assign('typeList', $this->model::getTypeList());
        $this->assignconfig('typeList', $this->model::getTypeList());

        $this->view->assign('statusList', $this->model::getStatusList());
        $this->assignconfig('statusList', $this->model::getStatusList());
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->with(['member_data' => ['group_data'], 'activity_data'])
                ->field('feedback.*')
                ->join([Member::getTableName() => 'member'], 'member.id=feedback.member_id', 'LEFT')
                ->where($where)->order($sort, $order)->limit($offset, $limit)->paginate($limit);
            foreach ($list->items() as &$item) {
                $item->content = Super::substrText($item->content, 24, '...');
            }
            $result = array(
                'total' => $list->total(),
                'rows'  => $list->items(),
            );

            return json($result);
        }

        // 活动列表
        $activityList = Activity::getOptionList();
        $this->view->assign('activityList', $activityList);
        $this->assignconfig('activityList', $activityList);

        return $this->view->fetch();
    }
}
